package mobi.bantu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import mobi.bantu.bean.Cons;
import mobi.bantu.bean.Page;
import mobi.bantu.service.SysConfigService;
import mobi.bantu.bean.SysConfig;
import mobi.bantu.dao.mapper.SysConfigMapper;

/**
 * Created by qiao on 2017/5/11.
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public List<SysConfig> listByType(String type) {
        return sysConfigMapper.listByType(type);
    }

    @Override
    public Page<SysConfig> page(Map<String, Object> sp) {
        return Page.create(sysConfigMapper.list(sp), sysConfigMapper.count(sp));
    }

    @Override
    public SysConfig getById(Long id) {
        return sysConfigMapper.get(id);
    }

    @Override
    public int update(SysConfig sysConfig) {
        return sysConfigMapper.update(sysConfig);
    }

    @Override
    public int delete(Long id) {
        return sysConfigMapper.delete(id);
    }

    @Override
    public int add(SysConfig sysConfig) {
        return sysConfigMapper.create(sysConfig);
    }

    @Override
    public String getValue(String type, String name) {
        return sysConfigMapper.selectByTypeName(type, name);
    }

    @Override
    public Map<SysConfig, List<SysConfig>> buildDepArea() {
        List<SysConfig> deps = sysConfigMapper.listByType(Cons.DEP);
        if (CollectionUtils.isEmpty(deps)) return null;
        Map<SysConfig,List<SysConfig>> result = new HashMap<>();
        for (SysConfig sysConfig : deps) {
            result.put(sysConfig , getByName(Cons.DEP + sysConfig.getTitle()));
        }
        return result;
    }

    private List<SysConfig> getByName(String title) {
        return sysConfigMapper.listByTitle(title);
    }
}
